package com.android.fc;

import com.android.application.ForecastApplication;
import com.forecasting.model.PartNumber;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class EnterSkuActivity extends Activity {
	EditText uid ;
	EditText name ;
	EditText desc ;
	EditText fc ;
	EditText lt ;
	EditText rop ; 
	EditText std ;
	
	private ForecastApplication forecast;
	
	
	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.sku);
		//create editable field from GUI
		uid = (EditText)findViewById(R.id.uidInput);
		name = (EditText)findViewById(R.id.nameInput);
		desc = (EditText)findViewById(R.id.descInput);
		fc =   (EditText)findViewById(R.id.fcInput);
		lt =   (EditText)findViewById(R.id.ltInput); 
		rop =   (EditText)findViewById(R.id.ropInput); 
		std =   (EditText)findViewById(R.id.stdInput); 
		
		forecast = (ForecastApplication)getApplication();
		
		//Button button = (Button)findViewById(R.id.calcBtn);

	}
	public void calculateHandler(View target) {
		PartNumber pn = null;
		String toastmsg = "Please complete the following field: ";
		String addStr = " has been added to the list of UID.";
		String noAddStr = " already exist in the list of UID.";

		if(uid.getText().toString().length() == 0){
			Toast toast = Toast.makeText(getApplicationContext(), toastmsg + "UID",Toast.LENGTH_SHORT);
			toast.show();

		}else if(fc.getText().toString().length() == 0){
			Toast toast = Toast.makeText(getApplicationContext(), toastmsg + "Forecast",Toast.LENGTH_SHORT);
			toast.show();
		}else if(lt.getText().toString().length() == 0){
			Toast toast = Toast.makeText(getApplicationContext(), toastmsg + "Leadtime",Toast.LENGTH_SHORT);
			toast.show();
		}else if(rop.getText().toString().length() == 0){
			Toast toast = Toast.makeText(getApplicationContext(), toastmsg + "Reorder Point",Toast.LENGTH_SHORT);
			toast.show();
		}
		else{
			if(std.getText().toString().length() == 0){
				pn = new PartNumber(uid.getText().toString(),
						name.getText().toString(),
						desc.getText().toString(),
						Double.parseDouble(fc.getText().toString()),
						Double.parseDouble(lt.getText().toString()),
						Integer.parseInt(rop.getText().toString()),
						0);
			}else{
				pn = new PartNumber(uid.getText().toString(),
						name.getText().toString(),
						desc.getText().toString(),
						Double.parseDouble(fc.getText().toString()),
						Double.parseDouble(lt.getText().toString()),
						Integer.parseInt(rop.getText().toString()),
						Double.parseDouble(std.getText().toString())
						);
			}
			Toast toast = null;
			//add PN to SortedSet from application
			if(forecast.getPNList().add(pn)){

				toast = Toast.makeText(getApplicationContext(),(pn.getUniqueID() + addStr),
						Toast.LENGTH_LONG);
			}else{
				toast = Toast.makeText(getApplicationContext(),(pn.getUniqueID() + noAddStr),
						Toast.LENGTH_LONG);
			}
			toast.show();
			resetButton();
		}	
		
	}
	private void resetButton(){
		uid.setText(null);
		name.setText(null);
		desc.setText(null);
		fc.setText(null);
		lt.setText(null);
		rop.setText(null);
		std.setText(null);
		
	}
}